﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;
using System.Configuration;
using System.IO;

namespace PPAP.Web.SearchReport
{
    public partial class FilesSearch : PageBase
    {
       T_FileBO tbo = new T_FileBO ();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnSubmit_Click(null, null);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql = " and 1=1 ";
            if (txtPPARNo.Text.Trim() != "")
                sql += " and pparno like '%" + txtPPARNo.Text.Trim() + "%' ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and r.bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtUser.Text.Trim() != "")
                sql += " and t.upby like '%" + txtUser.Text.Trim() + "%' ";
            if (txtStart.Text.Trim() != "")
                sql += " and trunc(t.uptime)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtEnd.Text.Trim() != "")
                sql += " and trunc(t.uptime)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (DropDownList1.SelectedValue != "All")
                sql += " and t.filetype='"+DropDownList1.SelectedValue+"' ";
            DataSet ds = tbo.GetListForSearch(sql);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        public static DataTable GetPageTable(DataTable dt, int pageSize, int pageIndex)
        {
            if (dt != null)
            {
                DataTable dt1 = new DataTable();

                int pageEnd = pageIndex * pageSize;
                int pagebegin = (pageIndex - 1) * pageSize;


                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt1.Columns.Add(new DataColumn(dt.Columns[i].ColumnName));
                }

                for (int i = pagebegin; i < pageEnd; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        dt1.ImportRow(dt.Rows[i]);
                    }

                }
                return dt1;
            }
            else
                return null;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sql = " and 1=1 ";
            if (txtPPARNo.Text.Trim() != "")
                sql += " and pparno like '%" + txtPPARNo.Text.Trim() + "%' ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtUser.Text.Trim() != "")
                sql += " and upby like '%" + txtUser.Text.Trim() + "%' ";
            if (txtStart.Text.Trim() != "")
                sql += " and trunc(uptime)>=to_date( '" + txtStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtEnd.Text.Trim() != "")
                sql += " and trunc(uptime)<=to_date( '" + txtEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (DropDownList1.SelectedValue != "All")
                sql += " and filetype='" + DropDownList1.SelectedValue + "' ";
            DataSet ds = tbo.GetListForSearch(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["d"] = r["isdelete"].ToString()=="1"?"Yes":"No";
                r["d1"] = r["UpTime"].ToString() == "" ? r["UpTime"] : DateTime.Parse(r["UpTime"].ToString()).ToString("yyyy-MM-dd");
             }
            ds.Tables[0].Columns.Remove("UPTIME");
            ds.Tables[0].Columns.Remove("F_FILENAME");
            ds.Tables[0].Columns.Remove("ISDELETE");

            ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
            ds.Tables[0].Columns["ATTACHMENTTYPE"].ColumnName = "Attach.type";
            ds.Tables[0].Columns["UPBY"].ColumnName = "Uploader";
            ds.Tables[0].Columns["d1"].ColumnName = "Upload time";
            ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR No.";
            ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part No.";
            ds.Tables[0].Columns["FILETYPE"].ColumnName = "Type";
            ds.Tables[0].Columns["FILESIZE"].ColumnName = "Size";
            ds.Tables[0].Columns["D"].ColumnName = "IsDelete";
            ds.Tables[0].Columns["F_URL"].ColumnName = "Link";

            CreateExcel(ds, "FilesSearch");
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetLink(string filename)
        {
            //string filepath = "ftp://" + ConfigurationManager.AppSettings["ftpServerIP"] + "/";
            string filepath = ConfigurationManager.AppSettings["FilePath"];
            return filepath + filename;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            if (File.Exists(ConfigurationManager.AppSettings["FilePath"] + link.Text))
            {
                FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + link.Text);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                Response.AddHeader("Content-length", file.Length.ToString());
                Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
        }
    }
}